<?php
/**
 * @author fld 2021-09-11
 * @desc 图片业务类
 */
namespace App\Service\Image;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ImageService
{
    private $module_name = '图片管理';

    public function __construct()
    {

    }

    /**
     * @author fld 2021-09-11
     * @desc 通过条件获取一条图片分组数据
     * @return mixed
     */
    public function getImgGroupByCondition($condition){
        $obj = DB::table('img_group')->where($condition)->first();
        return objectToArray($obj);
    }
    /**
     * @author fld 2021-09-11
     * @desc 获取图片分组列表
     * @return mixed
     */
    public function getImgGroupList(){
        $params = request()->all();
        $query = DB::table('img_group');
        if(isset($params['group_name']) && !empty(trim($params['group_name']))){
            $query = $query->where('group_name', 'like', "%{$params['group_name']}%");
        }
        $list = $query->orderBy('sortby','asc')->orderBy('id','asc')->get();
        return objectToArray($list);
    }
    /**
     * @author fld 2021-09-11
     * @desc 保存图片分组信息
     * @return number[]|string[]
     */
    public function add_group(){
        $params = request()->all();
        $id = $params['id'];
        $group_name = trim($params['group_name']);
        $sortby = trim($params['sortby']);
        if(empty($group_name)){
            return ['code' => 1,'message' => '分组名称不能为空'];
        }
        if(empty($sortby) || !preg_match("/^[1-9][0-9]*$/" ,$sortby)){
            return ['code' => 1,'message' => '排序不能为空且必须为正整数'];
        }
        $insert_data = [];
        $insert_data['group_name'] = $group_name;
        $insert_data['sortby'] = $sortby;
        if(empty($id)){
            $res = DB::table('img_group')->insertGetId($insert_data);
        }else{
            $res = DB::table('img_group')->where('id',$id)->update($insert_data);
        }
        if($res !== false){
            return ['code' => 0,'message' => '图片分组保存成功'];
        }else{
            return ['code' => 1,'message' => '图片分组保存失败'];
        }
    }
    /**
     * @author fld 2021-09-11
     * @desc 删除图片分组信息
     * @param unknown $id
     * @return number[]|string[]
     */
    public function delete_group($id){
        if($id == 1){
            return ['code' => 0,'message' => '无法删除默认分组'];
        }
        $obj = $this->getImgGroupByCondition(['id'=>$id]);
        $res = DB::table('img_group')->where('id', $id)->delete();
        if($res !== false){
            //删除成功后将图片转为默认分组
            DB::table('img')->where('group_id', $id)->update(['group_id'=>1]);
            addActionLog(['module_name'=>$this->module_name,'type'=>'delete','log_remark'=>'删除编号:'.$id.',分组名称:'.$obj['group_name'].'信息']);
            return ['code' => 0,'message' => '删除图片分组成功'];
        }else{
            return ['code' => 1,'message' => '删除图片分组失败'];
        }
    }
    
    /**
     * @author fld 2021-09-11
     * @desc 更新图片分组排序
     * @param unknown $id
     * @return number[]|string[]
     */
    public function update_group(){
        $params = request()->all();
        $id = $params['id'];
        $ids = explode(',', $id);
        foreach($ids as $k=>$v){
            $sortby = $k+1;
            DB::table('img_group')->where('id',$v)->update(['sortby'=>$sortby]);
        }
        return ['code' => 0];
    }
    
    /**
     * @author fld 2021-09-13
     * @desc 获取图片列表
     * @return mixed
     */
    public function getImgList($group_id){
        $params = request()->all();
        $query = DB::table('img');
        //图片分组查询
        if(!empty($params['group_id'])){
            $query = $query->where('group_id', '=', $params['group_id']);
            $group_id = $params['group_id'];
        }else{
            if(!empty($group_id)){
                $query = $query->where('group_id', '=', $group_id);
            }else{
                $query = $query->where('group_id', '=', $group_id);
            }
        }
        //图片名称查询
        if(!empty($params['img_name'])){
            $query = $query->where('img_name', 'like', '%'.$params['img_name'].'%');
        }
        //排序
        if(!empty($params['sortby'])){
            if($params['sortby'] == 'time_asc'){
                $query = $query->orderBy('add_time','asc');
            }else if($params['sortby'] == 'time_desc'){
                $query = $query->orderBy('add_time','desc');
            }else if($params['sortby'] == 'name_asc'){
                $query = $query->orderBy('img_name','asc');
            }else if($params['sortby'] == 'name_desc'){
                $query = $query->orderBy('img_name','desc');
            }
        }else{
            $query = $query->orderBy('add_time','desc');
        }
        
        $list = $query->paginate(10);
        $pager = $list->links();
        return ['img_list' => $list, 'pager' => $pager, 'group_id' => $group_id];
    }
    /**
     * @author fld 2021-09-13
     * @desc 获取图片信息
     * @param unknown $id
     * @return mixed
     */
    public function getImgById($id){
        $obj = DB::table('img')->where('id',$id)->first();
        return objectToArray($obj);
    }
    /**
     * @author fld 2021-09-13
     * @desc 保存分组图片数据
     * @param unknown $data
     * @return number
     */
    public function addImg($data){
        $data['add_time'] = time();
        return DB::table('img')->insertGetId($data);
    }
    
    /**
     * @author fld 2021-09-13
     * @desc 删除图片信息
     * @param unknown $id
     * @return number[]|string[]
     */
    public function delete_img($id){
        $obj = $this->getImgById($id);
        $res = DB::table('img')->where('id', $id)->delete();
        if($res !== false){
            if(file_exists(public_path($obj['img_url']))){
                unlink(public_path($obj['img_url']));
            }
            addActionLog(['module_name'=>$this->module_name,'type'=>'delete','log_remark'=>'删除编号:'.$id.',图片名称:'.$obj['img_name'].'信息']);
            return ['code' => 0,'message' => '删除图片成功'];
        }else{
            return ['code' => 1,'message' => '删除图片失败'];
        }
    }
    /**
     * @author fld 2021-09-13
     * @desc 上传图片
     * @param Request $request
     * @return number[]|string[]|number[]|string[]|unknown[]
     */
    public function uploadImg(Request $request){
        $uploadService = new UploadService();
        $params = request()->all();
        $group_id = $params['group_id'];
        if(empty($group_id)){
            return ['code'=>1,'message'=>'图片分组id不存在'];
        }
        $group_obj = $this->getImgGroupByCondition(['id'=>$group_id]);
        if(empty($group_obj)){
            return ['code'=>1,'message'=>'图片分组不存在'];
        }
        $result = $uploadService->uploadImg($request, 'img_file');
        if($result['code'] == 0){
            $original_name = $result['original_name'];
            $new_path = $result['new_path'];
            $img_ext = $result['extension'];
            $img_data = [];
            $img_data['group_id'] = $group_id;
            $img_data['img_name'] = $original_name;
            $img_data['img_url'] = $new_path;
            $img_data['img_ext'] = $img_ext;
            $res = $this->addImg($img_data);
            if($res !== false){
                return ['code'=>0,'message'=>'上传图片成功'];
            }else{
                return ['code'=>1,'message'=>'图片保存失败'];
            }
        }else{
            return ['code'=>1,'message'=>$result['message']];
        }
    }
}